/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package batailledesardennes.entities.common;

import batailledesardennes.game.GameConstants;
import batailledesardennes.game.GameEnv;
import batailledesardennes.game.entity.BaseEntity;
import batailledesardennes.game.entity.collision.EntityCollsionManager;
import batailledesardennes.game.entity.drawer.AnimatedEntityDrawer;
import batailledesardennes.game.entity.drawer.BaseEntityDrawer;
import batailledesardennes.game.interfaces.Drawable;
import batailledesardennes.game.interfaces.Updatable;
import batailledesardennes.util.geomFloat.Point;
import batailledesardennes.util.geomFloat.Rectangle;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

/**
 *
 * @author cghislai
 */
public class Tree extends BaseEntity implements Updatable, Drawable{

    protected String[] imageFiles;
    protected int treeId;
    protected AnimatedEntityDrawer entityDrawer;
    protected EntityCollsionManager collsionManager;

    public Tree(String[] filePath, Point imageOffset, int treeId) {
        this.imageFiles = filePath;
        this.treeId = treeId;

        entityDrawer = new AnimatedEntityDrawer();
        entityDrawer.setEntity(this);
        entityDrawer.setImageOffset(imageOffset);


        Rectangle rect = new Rectangle(-2, -2, 4, 4);
        collsionManager = new EntityCollsionManager();
        collsionManager.setEntity(this);
        collsionManager.setBounds(rect);

        pos = new Point();
        selectable = false;
        collidable = true;
        drawable = true;
        entityTypeId = GameConstants.ENTITY_TYPE_ID_TREE;
    }

    @Override
    public void register(GameEnv env) {
        this.env = env;
        env.getEntitiesManager().addEntity(this);
        env.getUpdatableManager().addUpdatable(this);
        try {
            Animation anim = new Animation(false);
            for (String path : imageFiles) {
                Image image = new Image(path);
                anim.addFrame(image, 200);
            }
            entityDrawer.setCurrentAnim(anim);
        } catch (SlickException ex) {
            Logger.getLogger(Tree.class.getName()).log(Level.SEVERE, null, ex);
        }
        collsionManager.register();
    }

    @Override
    public void unregister() {
        env.getEntitiesManager().removeEntity(this);
        env.getUpdatableManager().removeUpdatable(this);
        collsionManager.unregsiter();
    }

    @Override
    public Rectangle getBounds() {
        return collsionManager.getBounds().getBoundsRect();
    }

    @Override
    public void draw(Graphics g) {
        entityDrawer.draw(g);
    }

    @Override
    public void update(int delta) {
        entityDrawer.update(delta);
    }

    @Override
    public short getDrawablePriority() {
        return GameConstants.DRAWABLE_PRIORITY_FRONT;
    }
    
    
}
